package bus.view;



import javax.swing.*;

import bus.dao.UserDao;
import bus.moudle.User;
import bus.util.DButil;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;
import java.awt.*;



public class RegisterForm extends JFrame implements ActionListener{

	JPanel panel;
    JLabel title, FullName, Password, Contact, Age, Gender, EmailID, Address;
    JTextField a, b, c, d, e, f, g;
    JButton register,next;
    String A,B,C,D,E,F,G; 

    RegisterForm() {


        panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(300,30, 700, 600);
        add(panel);

        title = new JLabel("User Registration");
        title.setBounds(100, 50, 300, 40);
        title.setForeground(Color.red);
        title.setFont(new Font("Arial", Font.BOLD, 25));


        FullName=new JLabel("Full_Name:");
        FullName.setBounds(100,90,150,40);
        FullName.setFont(new Font("Arial", Font.PLAIN, 20));


        a=new JTextField();
        a.setBackground(Color.yellow);
        a.setBounds(260,95,200,30);
        a.setFont(new Font ("Arial", Font.PLAIN,20));


        Password=new JLabel("Password:");
        Password.setBounds(100,140,100,40);
        Password.setFont(new Font("Arial", Font.PLAIN, 20));


        b=new JTextField();
        b.setBackground(Color.yellow);
        b.setBounds(260,145,200,30);
        b.setFont(new Font ("Arial", Font.PLAIN,20));


        Contact=new JLabel("Contact:");
        Contact.setBounds(100,190,100,40);
        Contact.setFont(new Font("Arial", Font.PLAIN, 20));


        c=new JTextField();
        c.setBackground(Color.yellow);
        c.setBounds(260,195,200,30);
        c.setFont(new Font ("Arial", Font.PLAIN,20));


        Age=new JLabel("Age:");
        Age.setBounds(100,240,100,40);
        Age.setFont(new Font("Arial", Font.PLAIN, 20));


        d=new JTextField();
        d.setBackground(Color.yellow);
        d.setBounds(260,245,200,30);
        d.setFont(new Font ("Arial", Font.PLAIN,20));


        Gender=new JLabel("Gender:");
        Gender.setBounds(100,290,100,40);
        Gender.setFont(new Font("Arial", Font.PLAIN, 20));


        e=new JTextField();
        e.setBackground(Color.yellow);
        e.setBounds(260,295,200,30);
        e.setFont(new Font ("Arial", Font.PLAIN,20));


        EmailID =new JLabel("Email_ID:");
        EmailID.setBounds(100,340,100,40);
        EmailID.setFont(new Font("Arial", Font.PLAIN, 20));


        f=new JTextField();
        f.setBackground(Color.yellow);
        f.setBounds(260,345,200,30);
        f.setFont(new Font ("Arial", Font.PLAIN,20));


        Address=new JLabel("Address:");
        Address.setBounds(100,390,100,40);
        Address.setFont(new Font("Arial", Font.PLAIN, 20));

        g=new JTextField();
        g.setBackground(Color.yellow);
        g.setBounds(260,395,200,30);
        g.setFont(new Font ("Arial", Font.PLAIN,20));


        register=new JButton("Register");
        register.setBounds(200,450,150,30);
        register.setFont(new Font ("Arial", Font.PLAIN,20));


        next=new JButton("Next");
        next.setBounds(420,450,100,30);
        next.setFont(new Font ("Arial", Font.PLAIN,20));


        panel.add(title);
        panel.add(FullName);
        panel.add(a);
        panel.add(Password);
        panel.add(b);
        panel.add(Contact);
        panel.add(c);
        panel.add(Age);
        panel.add(d);
        panel.add(Gender);
        panel.add(e);
        panel.add(EmailID);
        panel.add(f);
        panel.add(Address);
        panel.add(g);
        panel.add(register);
        panel.add(next);
        
        register.addActionListener(this);
        next.addActionListener(this);

        setTitle("User Registration");
        setBounds(300,30,700, 600);
        setVisible(true);
        setLayout(null);
    }


    public static void main(String[] args)  {
        RegisterForm rf=new RegisterForm();
        
    }


    @Override
    public void actionPerformed(ActionEvent ae) {
    	try {
    		 DButil dbUtil=new DButil();
    		 Connection con=dbUtil.getCon();
    		 dbUtil.getCon();
    	        G=g.getText();
    	        F=f.getText();
    	        E=e.getText();
    	        D=d.getText();
    	        C=c.getText();
    	        B=b.getText();
    	        A=a.getText();
    	        int d=0;
    	     User user=new User(A,B,"member",A,C,D,E,F,G);
            UserDao u=new UserDao();
            String reg1 = "1[3456789]\\d{9}";
            boolean n1 = C.matches(reg1); 
            String reg2="\\w{1,}@(\\w+\\.)+\\w+";
            boolean n2=F.matches(reg2);
        if(ae.getActionCommand()=="Register") {
            if(u.ifBlank(user)) {
                JOptionPane.showMessageDialog(this, "Information can't blank !!");
        
            }
            if(n1==false) {  
            	JOptionPane.showMessageDialog(this, "Please input correct Telnum");
        	} 
            else if(n2==false) {
            	JOptionPane.showMessageDialog(this, "Please input correct e_mail");
            }
            
            else {
            	if(u.isUserExist(con,user)) {
            		JOptionPane.showMessageDialog(this, "This user exist");
            	}
            	else {
            	d=u.userAdd(con, user);
            	if(d!=0)  JOptionPane.showMessageDialog(this, "Registe success.\nClick 'next' to bookticket");
            	else  JOptionPane.showMessageDialog(this, "Register failure");
            	}
            }
            
            }
        
        else if(ae.getActionCommand()=="Next") {
        	if(!u.ifBlank(user)) {
        	   new Ticket(A);this.dispose();
        	}
        	else
        	{
        		JOptionPane.showMessageDialog(this, "Please Registe.");
        	}
        }
    	}
    	catch(Exception e1) {
    		e1.printStackTrace();
    		System.out.print(e1);
    	}
    
    }
}